package com.slz3.business.module.configManagement.conZoneManage.steward;

import com.slz3.business.frame.enums.AuditFlag;
import com.slz3.business.frame.enums.UsingFlag;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.response.RR;
import com.slz3.business.frame.response.ResInfo;
import com.slz3.business.frame.steward.Steward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.configManagement.conZoneManage.domain.ConZoneManage;
import com.slz3.business.module.configManagement.conZoneManage.domain.ConZoneManageFactory;
import org.springframework.data.domain.Page;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.List;

public class ConZoneManageResExecutor {

    static Mono<ServerResponse> add(ServerRequest req, ConZoneManage conZoneManage) {
        // redundancy
        try {
            ConZoneManageSteward.redundancy(conZoneManage);
        } catch (Exception e) {
            RR.fail(req, ResInfo.builder().status(200).code("fail").theme(RR.EXCEPTION).description("获取关联对象信息失败").build());
        }
        //
        ConZoneManage conZoneManageDBLatest = Services.conZoneManage.save(conZoneManage);
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(conZoneManageDBLatest).build());
    }

    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> delete(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManage.delete(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("delete fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> modify(ServerRequest req, ConZoneManage source) {
        ConZoneManage target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            Steward.merge(source, target);
            // allow set empty
            target.setSerImgUrl(source.getSerImgUrl());
            target.setSerNameBgImgUrl(source.getSerNameBgImgUrl());
            // redundancy
            try {
                ConZoneManageSteward.redundancy(target);
            } catch (Exception e) {
                RR.fail(req, ResInfo.builder().status(200).code("fail").theme(RR.EXCEPTION).description("获取关联商品信息失败").build());
            }
            ConZoneManage conZoneManageDBLatest = Services.conZoneManage.modify(target);
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(conZoneManageDBLatest).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> list(ServerRequest req) {
        Page<ConZoneManage> conZoneManagePage = Services.conZoneManage.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(conZoneManagePage).build());
    }

    static Mono<ServerResponse> info(ServerRequest req) {
        ConZoneManage target = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (target != null) {
            return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domain(target).build());
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    static Mono<ServerResponse> choice(ServerRequest req) {
        Page<ConZoneManage> conZoneManagePage = Services.conZoneManage.page(req.queryParams());
        return RR.success(req, ResInfo.builder().code(RR.SUCCESS).theme("page").page(conZoneManagePage).build());
    }

    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> auditSubmit(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            conZoneManageList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.WAIT_AUDIT);
                ConZoneManageFactory.initBaseFields(item);
            });
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManageTransactional.batchModify(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditSubmit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> audit(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            conZoneManageList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.APPROVED);
                ConZoneManageFactory.initBaseFields(item);
            });
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManageTransactional.batchModify(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("audit fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> auditReject(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            conZoneManageList.stream().forEach(item -> {
                item.setSerAudit(AuditFlag.REJECT);
                ConZoneManageFactory.initBaseFields(item);
            });
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManageTransactional.batchModify(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> usingEnable(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            conZoneManageList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.ENABLE);
                ConZoneManageFactory.initBaseFields(item);
            });
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManageTransactional.batchModify(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("auditReject fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }


    /**
     * known: conZoneManageList is not null
     */
    public static Mono<ServerResponse> usingDisable(ServerRequest req) {
        List<ConZoneManage> conZoneManageList = RR.attribute(req, RR.RELAY_SAFE_VALID);
        if (VE.isNotEmpty(conZoneManageList)) {
            conZoneManageList.stream().forEach(item -> {
                item.setSerUsing(UsingFlag.DISABLE);
                ConZoneManageFactory.initBaseFields(item);
            });
            List<ConZoneManage> conZoneManageListNew = Services.conZoneManageTransactional.batchModify(conZoneManageList);
            if (VE.isNotEmpty(conZoneManageListNew) && conZoneManageListNew.size() == conZoneManageList.size()) {
                return RR.success(req, ResInfo.builder().code(RR.SUCCESS).domains(conZoneManageListNew).build());
            } else {
                return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("usingDisable fail").build());
            }
        } else {
            return RR.fail(req, ResInfo.builder().code(RR.EXECUTOR).theme("cannot find entity").build());
        }
    }

}
